<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-input placeholder="请输入内容" v-model.trim="queryInfo.query" class="input-with-select" clearable  @clear="init">
                        <el-button slot="append" icon="el-icon-search" @click="init"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" icon="el-icon-plus" @click="goAddPage">添加商品</el-button>
                </el-col>
            </el-row>
            <template>
                <el-table
                        border
                        :data="goodsData"
                        stripe
                        style="width: 100%"
                        v-loading="is_load"
                >
                    <el-table-column type="index" label="#"></el-table-column>
                    <el-table-column
                            align="center"
                            prop="goods_name"
                            label="商品名称"
                            width="600"
                    >
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="goods_price"
                            label="价格(元)"
                            width="180">
                        <template v-slot="scope">
                            <span>{{scope.row.goods_price.toFixed(2)}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="goods_weight"
                            label="商品重量"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            align="center"
                            prop="add_time"
                            label="创建时间">
                        <template v-slot="scope">
                            <span>{{scope.row.add_time | filterTime}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center">
                        <el-button type="primary" size="mini" icon="el-icon-plus">编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
                    </el-table-column>
                </el-table>
            </template>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[5, 10, 20, 50]"
                    :page-size="100"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="goodsTotal">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
    import $axios from '../../network/request'
    import moment from 'moment'//导入文件
    export default {
        name: "Goods",
        data() {
            return {
                search: '',
                queryInfo: {
                    query: '',
                    pagenum: 1,
                    pagesize: 5
                },
                goodsTotal: 0,
                goodsData: []
            }
        },
        created() {
            this.init()
        },
        methods: {
            init() {
                $axios({
                    url: '/goods',
                    params: this.queryInfo
                }).then(res => {
                    const {data} = res
                    if (data.meta.status !== 200) {
                        return this.$message.error("获取商品列表失败")
                    }
                    this.goodsTotal = data.data.total
                    this.goodsData = data.data.goods
                   this.$message.success("获取商品列表成功")

                })
            },
            handleCurrentChange(newPageNum) {
                this.queryInfo.pagenum = newPageNum;
                this.init();
            },
            handleSizeChange(newPageSize) {
                this.queryInfo.pagesize = newPageSize;
                this.init()
            },
            goAddPage(){
                this.$router.push("/goods/add")
            }

        },
        computed: {
            is_load() {
                return this.$store.state.is_loading
            }
        },
        filters: {
            filterTime(val) {
                return moment(val).format('YYYY-MM-DD HH:mm:ss')
            }
        }
    }
</script>

<style scoped>

</style>
